Skip to content

fix(ShaderConverter): read the subprograms of 2021.2+ shader blobs - #385

Open
nichinichisou0609 wants to merge 2 commits into
K0lb3:masterfrom
nichinichisou0609:codex/shader-segments
Open

nichinichisou0609 wants to merge 2 commits into
K0lb3:masterfrom
nichinichisou0609:codex/shader-segments

Conversation

@nichinichisou0609

@nichinichisou0609 nichinichisou0609 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Two problems in how ShaderProgram reads a platform's compressed blob, one commit each. Both make Shader.export() raise struct.error.

Multi-segment blobs. From Unity 2019.3 a blob can be split into segments, each compressed separately, and every entry of the index table names the segment its record is in. Export only decompressed segment 0 and read every offset from it, so a record in segment 1 or 2 was read from the wrong bytes or past the end. Every segment is now decompressed, and each entry is read at its offset inside the segment it names (older entries have no segment field and use segment 0). Single-segment shaders work as before, the two-argument ShaderProgram constructor is kept, and mismatched segment arrays or a reference to a missing segment raise ValueError.

For reference, AssetStudio decompresses each segment separately and reads each entry at its offset inside the referenced segment.

Parameter records. Since Unity 2021.2 the index table also points at parameter records, which have a different layout from subprogram records and are only told apart through the parsed form. ShaderProgram parsed every entry as a subprogram up front, so a parameter record was read as keywords and strings until it ran off the end. ShaderProgram now keeps the index table and reads a record only when a subprogram asks for it, through get_subprogram(index); this replaces the eagerly filled m_SubPrograms list. Among Unity's own shaders this hits UI/DefaultETC1 and Hidden/VideoDecode in a 2022.3 player build.

Samples (attached, built with Unity 2022.3.62f2):

sample master first commit both commits
shader_segments: one shader, GLES3 blob in 3 segments fails exports exports
unity_builtin_extra of a Windows player, 42 shaders 2 fail 2 fail all export

Not changed here: in 2021.2+ player builds the passes reference their programs through m_PlayerSubPrograms rather than m_SubPrograms, so the exported text of those shaders still has no program code, as on master.

…erenced

Since Unity 2021.2 (record version 202012090) a platform's shader blob
holds parameter records next to the subprogram records. The two have
different layouts, and only the parsed form tells them apart.
ShaderProgram parsed every index entry as a subprogram, so a parameter
record had its leading fields read as a keyword count and keyword
strings, and the export failed with struct.error past the end of the
blob. Among Unity's built-in shaders, UI/DefaultETC1 and
Hidden/VideoDecode in a 2022.3 player build fail this way.

ShaderProgram now keeps the index table and reads a record only when it
is requested through get_subprogram(), which replaces the eagerly
filled m_SubPrograms list.
@nichinichisou0609 nichinichisou0609 changed the title fix(ShaderConverter): read subprograms from their referenced segments fix(ShaderConverter): read the subprograms of 2021.2+ shader blobs Nov 16, 2026
@nichinichisou0609

nichinichisou0609 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Same as in #384/#391: removed the test module and rebased on master.

While checking this against real 2022.3 data I found that these shaders also fail for a second reason, in the same function, so I added it here as a second commit instead of opening another PR on top of this one. The description explains both. Happy to split it out if you prefer.

Both fixes have a sample in the zip, built from an empty Unity 2022.3 project (the build script and shader generator are included). The README lists which commit each one needs.

unitypy-385-samples.zip

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant